﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;
using System.Text;

using ExtAspNet;

namespace WebSite.admin
{
    public partial class index : BaseAdmin
    {

        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        readonly DALRoleRuleRef theRoleRuleRef = new DALRoleRuleRef();
        readonly DALCompany theCompany = new DALCompany();
        readonly DALRule theRule = new DALRule();
        public StringBuilder SbTreeHtml = new StringBuilder();



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FormBuild();
               // this.BindContent();
               // BindTreeList();
            }
        }



        void FormBuild()
        {


            int iAdminId =  int.Parse(new BaseAdmin().Administrator.AdminUserId);

            Dictionary<string, MainMenu> dicMain = new Dictionary<string, MainMenu>();
            Dictionary<string, SubMainMenu> dicSubMain = new Dictionary<string, SubMainMenu>();

            MainMenu oMainMenu = new MainMenu();
            SubMainMenu oSubMainMenu = new SubMainMenu();
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);

            if (oCompany == null)
            {
                DataSet dsRoleList = theRoleRuleRef.GetRoleListByAdminId(iAdminId);
                if (dsRoleList != null && dsRoleList.Tables.Count > 0)
                    foreach (DataRow drRole in dsRoleList.Tables[0].Rows)
                    {
                        int iRoleId = int.Parse(drRole["id"].ToString());
                        DataSet dsSub = theRoleRuleRef.GetMainListByRoleId(iRoleId);
                        DataSet ds1 = theRoleRuleRef.GetListByRoleId(iRoleId);
                        if (dsSub != null && dsSub.Tables.Count > 0 && ds1 != null && ds1.Tables.Count > 0)
                        {
                            foreach (DataRow drSub in dsSub.Tables[0].Rows)
                            {
                                if (!dicMain.ContainsKey(drSub["Description"].ToString()))
                                {
                                    oMainMenu = new MainMenu();
                                    oMainMenu.Id = drSub["RuleId"].ToString();
                                    oMainMenu.Name = drSub["Description"].ToString();
                                    oMainMenu.NavUrl = drSub["Url"].ToString();
                                    dicMain.Add(oMainMenu.Name, oMainMenu);
                                }

                                foreach (DataRow dr in ds1.Tables[0].Rows)
                                {

                                    if (drSub["RuleId"].ToString() == dr["ParentId"].ToString())
                                    {
                                        if (dr["Url"] != null && dr["Url"].ToString() != string.Empty)
                                        {
                                            if (!dicSubMain.ContainsKey(dr["Description"].ToString()))
                                            {
                                                oSubMainMenu = new SubMainMenu();
                                                oSubMainMenu.Id = dr["RuleId"].ToString();
                                                oSubMainMenu.Name = dr["Description"].ToString();
                                                oSubMainMenu.ParentId = drSub["RuleId"].ToString();
                                                oSubMainMenu.NavUrl = dr["Url"].ToString();
                                                dicSubMain.Add(oSubMainMenu.Name, oSubMainMenu);
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
            }

            else
            {
                if (oCompany.IsDelete)
                {
                    base.Response.Write("<script language=javascript>alert('贵公司账号已被禁用，如使用请与客服联系！');window.top.location.href='login.aspx';</script>");
                    base.Response.End();
                }

                DataSet dsMain = theRoleRuleRef.GetMainListByCompanyIdForCompany(oCompany.Id);

                DataSet ds0 = theRoleRuleRef.GetListByCompanyIdForCompany(oCompany.Id);
                if (dsMain != null && dsMain.Tables.Count > 0 && ds0 != null && ds0.Tables.Count > 0)
                {

                    foreach (DataRow drMain in dsMain.Tables[0].Rows)
                    {
                        if (!dicMain.ContainsKey(drMain["Description"].ToString()))
                        {
                            oMainMenu = new MainMenu();
                            oMainMenu.IsCompany = true;
                            oMainMenu.Id = drMain["RuleId"].ToString();
                            oMainMenu.Name = drMain["Description"].ToString();
                            oMainMenu.NavUrl = drMain["Url"].ToString();
                            dicMain.Add(oMainMenu.Name, oMainMenu);
                        }

                        foreach (DataRow dr in ds0.Tables[0].Rows)
                        {
                            if (drMain["RuleId"].ToString() == dr["ParentId"].ToString())
                            {
                                if (dr["Url"] != null && dr["Url"].ToString() != string.Empty)
                                {
                                    if (!dicSubMain.ContainsKey(dr["Description"].ToString()))
                                    {
                                        oSubMainMenu = new SubMainMenu();
                                        oSubMainMenu.IsCompany = true;
                                        oSubMainMenu.Id = dr["RuleId"].ToString();
                                        oSubMainMenu.Name = dr["Description"].ToString();
                                        oSubMainMenu.ParentId = drMain["RuleId"].ToString();
                                        oSubMainMenu.NavUrl = dr["Url"].ToString();
                                        dicSubMain.Add(oSubMainMenu.Name, oSubMainMenu);
                                    }

                                }
                            }
                        }
                    }
                }
            }

            foreach (MainMenu menu in dicMain.Values)
            {
                foreach (SubMainMenu subMenu in dicSubMain.Values)
                {
                    if (subMenu.ParentId == menu.Id && subMenu.IsCompany == menu.IsCompany)
                    {
                        string sDescription = subMenu.Name.Length > 5 ? subMenu.Name.Replace("列表", "") : subMenu.Name;
                        SbTreeHtml.Append("treeMenuAddItem(2, \"" + sDescription + "\", \"" + subMenu.NavUrl + "\", \"tabWin\",\"l4.gif\");\n");

                        BindTreeNode(menu.Name, menu.Name, sDescription, sDescription, subMenu.NavUrl);
                    }
                }

            }

            //BindTreeNode("个人设置", menu.Name, sDescription, sDescription, subMenu.NavUrl);
            //SbTreeHtml.Append("treeMenuAddItem(1, \"个人设置\");\n");
            //SbTreeHtml.Append("treeMenuAddItem(2, \"修改密码\", \"system/setPassword.aspx\", \"tabWin\",\"l4.gif\");\n");
            //SbTreeHtml.Append("</script>\n");
            //SbTreeHtml.Append("<script language=\"JavaScript\" type=\"text/jscript\">\n");
            //SbTreeHtml.Append("initializeDocument();\n");
            //SbTreeHtml.Append("clickOnNode(0);\n");
            //SbTreeHtml.Append("</script>\n");
        }



        //void BindTreeList()
        //{
        //    int iAdminId =  theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
        //    tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);
        //    if (oCompany != null)
        //    {
        //        base.Administrator.CompanyId = oCompany.Id.ToString();
        //        BindTreeNode("CardManager", "卡片管理", "OpenCardManger", "开卡管理", "product/OpenCard.aspx");
        //        BindTreeNode("CompanyManger", "商家管理", "AddCompany", "添加商家", "company/companyManager.aspx");
        //        BindTreeNode("CompanyManger", "公司管理", "CompanyList", "商家列表", "company/companyList.aspx");
        //    }
        //    DataSet ds;
        //    ds = theUserRuleRef.GetListByAdminId(iAdminId);
        //    if (ds != null && ds.Tables.Count > 0)
        //    {
              
        //        foreach (DataRow dr in ds.Tables[0].Rows)
        //        {
        //            ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                       
        //            switch (oERule)
        //            {
        //                case ERule.AddAdmin:

        //                    BindTreeNode("AdminManager", "管理员设置", "AddAdmin", "添加管理员", "user/adduser.aspx");
        //                    break;
        //                case ERule.AddArticle:

        //                    BindTreeNode("ArticleManager", "文章管理", "AddArticle", "添加文章", "title/addnewsclass.aspx");
        //                    break;
        //                case ERule.AddArticleClass:

        //                    BindTreeNode("ArticleManager", "文章管理", "AddArticleClass", "添加文章分类", "title/addnewsclass.aspx");
        //                    break;

        //                case ERule.AddCompany:

        //                    BindTreeNode("CompanyManger", "商家管理", "AddCompany", "添加商家", "company/companyManager.aspx");
        //                    break;
        //                case ERule.AddGiftCard:

        //                    BindTreeNode("CardManager", "卡片管理", "AddCard", "添加卡片", "product/card_add.aspx");
                          
        //                    break;
        //                case ERule.AddMenu:

        //                    BindTreeNode("SystemConfiguration", "系统设置", "AddMenu", "添加栏目", "system/navigator_edit.aspx");

        //                    break;
        //                case ERule.AddMessage:
        //                    BindTreeNode("SystemConfiguration", "系统设置", "AddMessage", "添加信息", "system/navigatora_edit.aspx");

        //                    break;
        //                case ERule.AddNewProduct:
        //                    BindTreeNode("ProductManager", "商品管理", "AddNewProduct", "添加商品","product/goods_add.aspx");
        //                    break;
        //                case ERule.AddOrder:
        //                    BindTreeNode("OrderManager", "订单管理", "AddOrder", "添加订单","order/OrderAdd.aspx");
        //                    break;
        //                //case ERule.MeterialOrderAdd:
        //                //    BindTreeNode("MeterialOrderAdd", "订单管理", "MeterialOrderAdd", "添加订单");
        //                //    this.MeterialOrderAdd.Visible = true;
        //                //    this.MeterialOrderAdd.Visible = true;
        //                //    break;
        //                case ERule.AddProductBrand:
        //                    BindTreeNode("ProductManager", "商品管理", "AddProductBrand", "添加商品品牌","product/brand_add.aspx");
        //                    break;
        //                case ERule.AddProductClass:
        //                    BindTreeNode("ProductManager", "商品管理", "AddProductClass", "添加商品类别","product/category_add.aspx");
        //                    break;
        //                case ERule.AddProfessionAreaType:
        //                    BindTreeNode("PromoteManager", "广告管理", "AddProfessionAreaType", "添加首页广告","shop/topic_add.aspx");
        //                    break;
        //                case ERule.AddPromoteProduct:
        //                    BindTreeNode("PromoteManager", "广告管理", "AddPromoteProduct", "添加左侧广告","shop/topics_add.aspx");
        //                    break;
        //                case ERule.AddUser:
        //                    BindTreeNode("UserManager", "用户管理", "AddUser", "添加用户","member/member_add.aspx");
        //                    break;
        //                case ERule.AdminList:
        //                    BindTreeNode("AdminManager", "管理员设置", "AdminList", "管理员列表","user/userlist.aspx");
        //                    break;
        //                case ERule.ArticleClass:
        //                    BindTreeNode("ArticleManager", "文章管理", "ArticleClass", "文章大类","title/addnewsclass.aspx");
        //                    break;
        //                case ERule.ArticleList:
        //                    BindTreeNode("ArticleManager", "文章管理", "ArticleList", "文章列表","title/news.aspx");
        //                    break;
        //                case ERule.CompainList:
        //                    BindTreeNode("OrderManager", "订单管理", "CompainList", "投诉列表","order/OrderComplain.aspx");
                          
        //                    break;
        //                case ERule.CompainType:
        //                    BindTreeNode("OrderManager", "订单管理", "CompainType", "投诉类别", "order/compain_type.aspx");
  
        //                    break;
        //                case ERule.CompanyList:
        //                    BindTreeNode("CompanyManger", "公司管理", "CompanyList", "商家列表","company/companyList.aspx");
        //                    break;
        //                case ERule.DefaultBulletin:
        //                    BindTreeNode("SystemConfiguration", "系统配置", "DefaultBulletion", "首页公告","system/bulletin.aspx");
        //                    break;
        //                case ERule.FlashAdvertConfiguration:
        //                    BindTreeNode("SystemConfiguration", "系统配置", "FlashConfiguration", "切换广告","banner.aspx");
        //                    break;
        //                case ERule.LeaveWordClass:
        //                    BindTreeNode("UserManager", "用户管理", "LeaveWordType", "留言类别","member/msg_type.aspx");
        //                    break;
                      
        //                case ERule.OpenCardManager:
        //                    BindTreeNode("CardManager", "卡片管理", "OpenCardManger", "开卡管理","product/OpenCard.aspx");
        //                    break;
        //                case ERule.OrderProcess:
        //                    BindTreeNode("OrderManager", "订单管理", "order_process", "订单处理","order/order_process.aspx");
        //                    break;
        //                case ERule.OrderProcessSelf:
        //                    BindTreeNode("OrderManager", "订单管理", "order_process", "订单处理", "order/order_process.aspx");
        //                    break;
        //                case ERule.OrderQuery:
        //                    BindTreeNode("OrderManager", "订单管理", "OrderQuery", "订单查询","order/order.aspx");
        //                    break;
        //                case ERule.OrderQuerySelf:
        //                    BindTreeNode("OrderManager", "订单管理", "OrderQuery", "订单查询", "order/order.aspx");
        //                    break;   
                       
        //                case ERule.ProductBrand:
        //                    BindTreeNode("ProductManager", "商品管理", "ProductBrand", "商品品牌","product/brand.aspx");
        //                    break;
                          
        //                case ERule.ProductClass:
        //                    BindTreeNode("ProductManager", "商品管理", "ProuctClass", "商品大类", "product/category.aspx");
        //                    break;
                        
        //                case ERule.ProductDefaultParament:
        //                    BindTreeNode("ProductManager", "商品管理", "ProductDefaultParament", "商品首页参数", "product/goods_type.aspx");
        //                    break;
                           
        //                case ERule.ProductList:
        //                    BindTreeNode("ProductManager", "商品管理", "ProductList", "商品列表","product/goods.aspx");
        //                    break;
                         
        //                case ERule.ProductRecycle:
        //                    BindTreeNode("ProductManager", "商品管理", "ProductRecycle", "商品回收站","product/goods_trash.aspx");
        //                    break;
                         
        //                case ERule.ProfessionManager:
        //                    BindTreeNode("PromoteManager", "广告管理", "ProfessionAreaTypeManager", "首页广告管理","shop/topic.aspx");
        //                    break;
                       
        //                case ERule.PromoteProductManager:
        //                    BindTreeNode("PromoteManager", "广告管理", "PromoteProductManager", "左侧广告管理","shop/topics.aspx");
        //                    break;                           
        //                case ERule.OrderStat:
        //                    BindTreeNode("ReporStat", "报表管理", "OrderStat", "订单统计","report/Report.aspx");
        //                    break;
        //                case ERule.CardStat:
        //                    BindTreeNode("ReporStat", "报表管理", "CardStat", "卡片统计","report/CardReport.aspx");
        //                    break;
     
        //                case ERule.SelftConfiurationMenu:
        //                    BindTreeNode("SystemConfiguration", "系统设置", "SelfConfigurationMenu", "自定义栏目","system/navigator.aspx");
        //                    break;
        //                case ERule.StoreConfiguration:
        //                    BindTreeNode("SystemConfiguration", "系统设置", "StoreConfiguration", "商店设置","system/shop_config.aspx");
        //                    break;
                         
        //                case ERule.UserComment:
        //                    BindTreeNode("ProductManager", "商品管理", "UserCommemnt", "用户评论","product/comment_manage.aspx");
        //                    break;
        //                case ERule.UserLeaveWord:
        //                    BindTreeNode("UserManager", "用户管理", "UserLeaveWord", "用户留言","member/member_msg.aspx");
        //                    break;
        //                case ERule.UserList:
        //                    BindTreeNode("UserManager", "用户管理", "UserList", "用户列表","member/member.aspx");
        //                    break;
        //                case ERule.CardType:
        //                    BindTreeNode("CardManager", "卡片管理", "CardType", "卡片类别","product/CardType.aspx");
        //                    break;
        //                case ERule.ProfessionAreaType:
        //                    BindTreeNode("SystemConfiguration", "系统配置", "ProfessionArea", "专区类别","system/ProfessionAreaType.aspx");
        //                    break;
        //                case ERule.CardQuery:
        //                    BindTreeNode("CardManager", "卡片管理", "CardQuery", "卡片查询","product/CardQuery.aspx");
        //                    break;
        //                case ERule.OrderCancel:
        //                    BindTreeNode("OrderManager", "订单管理", "OrderCancel", "取消订单","order/OrderCancel.aspx");
        //                    break;
        //                case ERule.OrderCancelSelf:
        //                    BindTreeNode("OrderManager", "订单管理", "OrderCancel", "取消订单", "order/OrderCancel.aspx");
        //                    break;

        //            }
        //        }
        //    }
        //}

        int  FindNodeById(string sNodeId)
        {
            for (int i = 0; i < this.treeMenu.Nodes.Count; i++)
            {
                if (this.treeMenu.Nodes[i].NodeID == sNodeId)
                {
                    return i;
                }
            }
            return -1;
         
         }

        void BindTreeNode(string parentId,string parentName,string subId,string subName,string url)
        {
            ExtAspNet.TreeNode node;
            int nodeIndex = FindNodeById(parentId);
            if (nodeIndex < 0)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = parentId;
                node.SingleClickExpand = true;
                node.Text = parentName;
                if (node.Text == "管理员")
                {
                    node.IconUrl = "images\\xp_icons\\00.gif";
                }
                else if (node.Text == "商城商品")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_195.ico";
                }
                else if (node.Text == "订单管理")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_217.ico";
                }
                else if (node.Text == "会员管理")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_208.ico";
                }
                else if (node.Text == "内容管理")
                {
                    node.IconUrl = "images\\xp_icons\\042.gif";
                }
                else if (node.Text == "消息管理")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_16.ico";
                }
                else if (node.Text == "网站设置")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_65.ico";
                }
                else if (node.Text == "商家管理")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_125.ico";
                }
                else if (node.Text == "卡券管理")
                {
                    node.IconUrl = "images\\Windows 7\\Sticky Notes.ico";
                }
                else if (node.Text == "报表统计")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_107.ico";
                }
                else if (node.Text == "仓库管理")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_25.ico";
                }
                else if (node.Text == "票据管理")
                {
                    node.IconUrl = "images\\Windows 7\\Icon_22.ico";
                }
                this.treeMenu.Nodes.Add(node);
                nodeIndex = FindNodeById(parentId);
            }

            node = new ExtAspNet.TreeNode();
            node.NodeID = subId;
            node.SingleClickExpand = true;
            node.NavigateUrl = url;
            node.IconUrl = "images\\Editor\\table_cf.gif";
            node.Text = subName;
            this.treeMenu.Nodes[nodeIndex].Nodes.Add(node);
        }

        // Methods
        private void BindContent()
        {
            //this.Label1.Text = count.counts("", "DB_order", "states", "0");
            //this.Label2.Text = count.counts("", "DB_order", "states", "1");
            //this.Label3.Text = count.counts("", "DB_order", "states", "2");
            //this.Label4.Text = count.counts("", "DB_order", "states", "3");
            ////   this.Label5.Text = count.counts("", "DB_order", "states", "3");
            //this.goods1.Text = count.countsb("", "DB_Shoppro", "visible", "1", "protype", "0");
            //this.goods2.Text = count.countsb("", "DB_Shoppro", "visible", "1", "protype", "1");
            //this.goods3.Text = count.counts("", "DB_Shoppro", "proisnew1", "1");
            //this.goods4.Text = count.counts("", "DB_Shoppro", "proisnew2", "1");
            //this.goods5.Text = count.counts("", "DB_Shoppro", "proisnew3", "1");
            //this.goods6.Text = count.counts("", "DB_Shoppro", "iscuxao", "1");
            //this.msg.Text = count.counts("", "DB_Msg", "1", "1");
            //this.comment.Text = count.counts("", "DB_Comment", "1", "1");
        }

        //void ValidateLoginRule()
        //{
        //    int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
        //    tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);

        //    DataSet ds;
        //    ds = theUserRuleRef.GetListByAdminId(iAdminId);
        //    if (ds != null && ds.Tables.Count > 0)
        //    {
        //        foreach (DataRow dr in ds.Tables[0].Rows)
        //        {
        //            ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
        //            if (oERule == ERule.OrderProcess)
        //            {

        //                this.tbOrder.Visible = true;
        //                this.BindContent();
        //                return;
        //            }

        //        }
        //    }


        //}
        protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
        {
            base.Session["Adminlogin"] = null;
            base.Session["Uid"] = null;
            base.Response.Write("<script language=javascript>window.top.location.href='login.aspx';</script>");
            base.Response.End();
        }
  
    }
}